<!DOCTYPE html><html lang="en"><head><title>CreatePayment</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="CreatePayment"><meta name="groc-project-path" content="CreatePayment.php"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">CreatePayment.php</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper"><span class="cp">&lt;?php</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h1 id="createpaymentsample">CreatePaymentSample</h1>

<p>This sample code demonstrate how you can process
a direct credit card payment. Please note that direct 
credit card payment and related features using the 
REST API is restricted in some countries.
API used: /v1/payments/payment</p></div></div><div class="code"><div class="wrapper"><span class="k">require</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/../bootstrap.php&#39;</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Amount</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Details</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Item</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\ItemList</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\CreditCard</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Payer</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Payment</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\FundingInstrument</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Transaction</span><span class="p">;</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="creditcard">CreditCard</h3>

<p>A resource representing a credit card that can be
used to fund a payment.</p></div></div><div class="code"><div class="wrapper"><span class="nv">$card</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">CreditCard</span><span class="p">();</span>
<span class="nv">$card</span><span class="o">-&gt;</span><span class="na">setType</span><span class="p">(</span><span class="s2">&quot;visa&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setNumber</span><span class="p">(</span><span class="s2">&quot;4417119669820331&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setExpireMonth</span><span class="p">(</span><span class="s2">&quot;11&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setExpireYear</span><span class="p">(</span><span class="s2">&quot;2019&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setCvv2</span><span class="p">(</span><span class="s2">&quot;012&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setFirstName</span><span class="p">(</span><span class="s2">&quot;Joe&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setLastName</span><span class="p">(</span><span class="s2">&quot;Shopper&quot;</span><span class="p">);</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="fundinginstrument">FundingInstrument</h3>

<p>A resource representing a Payer's funding instrument.
For direct credit card payments, set the CreditCard
field on this object.</p></div></div><div class="code"><div class="wrapper"><span class="nv">$fi</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">FundingInstrument</span><span class="p">();</span>
<span class="nv">$fi</span><span class="o">-&gt;</span><span class="na">setCreditCard</span><span class="p">(</span><span class="nv">$card</span><span class="p">);</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payer">Payer</h3>

<p>A resource representing a Payer that funds a payment
For direct credit card payments, set payment method
to 'credit_card' and add an array of funding instruments.</p></div></div><div class="code"><div class="wrapper"><span class="nv">$payer</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Payer</span><span class="p">();</span>
<span class="nv">$payer</span><span class="o">-&gt;</span><span class="na">setPaymentMethod</span><span class="p">(</span><span class="s2">&quot;credit_card&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setFundingInstruments</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nv">$fi</span><span class="p">));</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="itemized-information">Itemized information</h3>

<p>(Optional) Lets you specify item wise
information</p></div></div><div class="code"><div class="wrapper"><span class="nv">$item1</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Item</span><span class="p">();</span>
<span class="nv">$item1</span><span class="o">-&gt;</span><span class="na">setName</span><span class="p">(</span><span class="s1">&#39;Ground Coffee 40 oz&#39;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setCurrency</span><span class="p">(</span><span class="s1">&#39;USD&#39;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setQuantity</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setPrice</span><span class="p">(</span><span class="s1">&#39;7.50&#39;</span><span class="p">);</span>
<span class="nv">$item2</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Item</span><span class="p">();</span>
<span class="nv">$item2</span><span class="o">-&gt;</span><span class="na">setName</span><span class="p">(</span><span class="s1">&#39;Granola bars&#39;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setCurrency</span><span class="p">(</span><span class="s1">&#39;USD&#39;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setQuantity</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setPrice</span><span class="p">(</span><span class="s1">&#39;2.00&#39;</span><span class="p">);</span>

<span class="nv">$itemList</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ItemList</span><span class="p">();</span>
<span class="nv">$itemList</span><span class="o">-&gt;</span><span class="na">setItems</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nv">$item1</span><span class="p">,</span> <span class="nv">$item2</span><span class="p">));</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="additional-payment-details">Additional payment details</h3>

<p>Use this optional field to set additional
payment information such as tax, shipping
charges etc.</p></div></div><div class="code"><div class="wrapper"><span class="nv">$details</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Details</span><span class="p">();</span>
<span class="nv">$details</span><span class="o">-&gt;</span><span class="na">setShipping</span><span class="p">(</span><span class="s1">&#39;1.20&#39;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setTax</span><span class="p">(</span><span class="s1">&#39;1.30&#39;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setSubtotal</span><span class="p">(</span><span class="s1">&#39;17.50&#39;</span><span class="p">);</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="amount">Amount</h3>

<p>Lets you specify a payment amount.
You can also specify additional details
such as shipping, tax.</p></div></div><div class="code"><div class="wrapper"><span class="nv">$amount</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Amount</span><span class="p">();</span>
<span class="nv">$amount</span><span class="o">-&gt;</span><span class="na">setCurrency</span><span class="p">(</span><span class="s2">&quot;USD&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setTotal</span><span class="p">(</span><span class="s2">&quot;20.00&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setDetails</span><span class="p">(</span><span class="nv">$details</span><span class="p">);</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="transaction">Transaction</h3>

<p>A transaction defines the contract of a
payment - what is the payment for and who
is fulfilling it. </p></div></div><div class="code"><div class="wrapper"><span class="nv">$transaction</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Transaction</span><span class="p">();</span>
<span class="nv">$transaction</span><span class="o">-&gt;</span><span class="na">setAmount</span><span class="p">(</span><span class="nv">$amount</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setItemList</span><span class="p">(</span><span class="nv">$itemList</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setDescription</span><span class="p">(</span><span class="s2">&quot;Payment description&quot;</span><span class="p">);</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="payment">Payment</h3>

<p>A Payment Resource; create one using
the above types and intent set to sale 'sale'</p></div></div><div class="code"><div class="wrapper"><span class="nv">$payment</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Payment</span><span class="p">();</span>
<span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">setIntent</span><span class="p">(</span><span class="s2">&quot;sale&quot;</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setPayer</span><span class="p">(</span><span class="nv">$payer</span><span class="p">)</span>
  <span class="o">-&gt;</span><span class="na">setTransactions</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nv">$transaction</span><span class="p">));</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="create-payment">Create Payment</h3>

<p>Create a payment by calling the payment->create() method
with a valid ApiContext (See bootstrap.php for more on <code>ApiContext</code>)
The return object contains the state.</p></div></div><div class="code"><div class="wrapper"><span class="k">try</span> <span class="p">{</span>
  <span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">create</span><span class="p">(</span><span class="nv">$apiContext</span><span class="p">);</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">PayPal\Exception\PPConnectionException</span> <span class="nv">$ex</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">echo</span> <span class="s2">&quot;Exception: &quot;</span> <span class="o">.</span> <span class="nv">$ex</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">()</span> <span class="o">.</span> <span class="nx">PHP_EOL</span><span class="p">;</span>
  <span class="nb">var_dump</span><span class="p">(</span><span class="nv">$ex</span><span class="o">-&gt;</span><span class="na">getData</span><span class="p">());</span>
  <span class="k">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="cp">?&gt;</span><span class="x"></span>
<span class="x">&lt;html&gt;</span>
<span class="x">&lt;head&gt;</span>
<span class="x">  &lt;title&gt;Direct Credit card payments&lt;/title&gt;</span>
<span class="x">&lt;/head&gt;</span>
<span class="x">&lt;body&gt;</span>
<span class="x">  &lt;div&gt;</span>
<span class="x">    Created payment:</span>
<span class="x">    </span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">getId</span><span class="p">();</span><span class="cp">?&gt;</span><span class="x"></span>
<span class="x">  &lt;/div&gt;</span>
<span class="x">  &lt;pre&gt;</span><span class="cp">&lt;?php</span> <span class="nb">var_dump</span><span class="p">(</span><span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">toArray</span><span class="p">());</span><span class="cp">?&gt;</span><span class="x">&lt;/pre&gt;</span>
<span class="x">  &lt;a href=&#39;../index.html&#39;&gt;Back&lt;/a&gt;</span>
<span class="x">&lt;/body&gt;</span>
<span class="x">&lt;/html&gt;</span></div></div></div></div></body></html>